package objects;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;

import exception.skillexception;

import parser.Entry;

public class Entity {
	String entity_name;
	Long entity_id;
	//Hashtable<Long, ArrayList<entry>> entries;
	ArrayList<Entry> entries;
	ArrayList<String> skillname_list;
	
	public Entity(Long id, String name){
		this.entity_name = name;
		this.entity_id = id;
		this.entries = new ArrayList<Entry>();
		this.skillname_list = new ArrayList<String>();
	}
	
	public void add_entry(Entry one_entry){
		this.entries.add(one_entry);
		this.populate_skill_list(one_entry);
	}
	
	public int get_entries_numbers(){
		return this.entries.size();
	}

	@SuppressWarnings("unchecked")
	public void sort_entries(){
		Collections.sort(this.entries);
	}

	public String getEntity_name() {
		return entity_name;
	}

	public void setEntity_name(String entity_name) {
		this.entity_name = entity_name;
	}

	public Long getEntity_id() {
		return entity_id;
	}

	public void setEntity_id(Long entity_id) {
		this.entity_id = entity_id;
	}
	
	public void populate_skill_list(Entry one_entry){
		if(this.skillname_list.size()==0){
			this.skillname_list.add(one_entry.getMyskill().getSkillname());
		}else if(!this.skillname_list.contains(one_entry.getMyskill().getSkillname())){
			this.skillname_list.add(one_entry.getMyskill().getSkillname());
		}
	}
	public void printskills(){
		System.out.println(this.skillname_list.size());
		for(int i = 0; i<this.skillname_list.size();i++){
			System.out.println(this.skillname_list.get(i));
		}
	}
	
	
	public void print_skillactionnames(String skillname){
		Hashtable<Long, String> actionnames = new Hashtable<Long, String>();
		for(int i=0; i<this.entries.size();i++){
			if(this.entries.get(i).getMyskill().getSkillname()!=null){
				if(this.entries.get(i).getMyskill().getSkillname().equals(skillname)){
					Long id = this.entries.get(i).getMyskill().getSkillevent_action_id();
					if(id!=null){
						actionnames.put(id, this.entries.get(i).getMyskill().getSkillevent_action());
					}
				}
			}
		}
		Enumeration<Long> skill_id = actionnames.keys();
		Long id;
		System.out.println("liste d'action pour le skil "+skillname);
		while(skill_id.hasMoreElements()){
			id = skill_id.nextElement();
			System.out.println(id+" : "+actionnames.get(id));
		}
	}
	public void print_skillaction(String skillname){
		Hashtable<Long, String> actionnames = new Hashtable<Long, String>();
		for(int i=0; i<this.entries.size();i++){
			if(this.entries.get(i).getMyskill().getSkillname()!=null){
				if(this.entries.get(i).getMyskill().getSkillname().equals(skillname)){
					Long id = this.entries.get(i).getMyskill().getSkillevent_id();
					if(id!=null){
						actionnames.put(id, this.entries.get(i).getMyskill().getSkillevent());
					}
				}
			}
		}
		Enumeration<Long> skill_id = actionnames.keys();
		Long id;
		System.out.println("liste d'event pour le skil "+skillname);
		while(skill_id.hasMoreElements()){
			id = skill_id.nextElement();
			System.out.println(id+" : "+actionnames.get(id));
		}
	}
	public Hashtable<Long, String> get_eventlist(Hashtable<Long,String> list){
		Hashtable<Long,String> currentlist = list;
		for(int i=0; i<this.entries.size();i++){
			Long id = this.entries.get(i).getMyskill().getSkillevent_id();
			if(id!=null){
				currentlist.put(id, this.entries.get(i).getMyskill().getSkillevent());
			}
		}
		return currentlist;
	}
	public Hashtable<Long, String> get_event_actionlist(Hashtable<Long,String> list){
		Hashtable<Long,String> currentlist = list;
		for(int i=0; i<this.entries.size();i++){
			Long id = this.entries.get(i).getMyskill().getSkillevent_action_id();
			if(id!=null){
				currentlist.put(id, this.entries.get(i).getMyskill().getSkillevent_action());
			}
		}
		return currentlist;
	}
	public ArrayList<Entry> getskillwithaction(Long action){
		ArrayList<Entry> skills = new ArrayList<Entry>();
		for(int i=0; i<this.entries.size();i++){
			if(/*(this.entries.get(i).getMyskill().getSkillevent_id()!=null)&&*/(this.entries.get(i).getMyskill().getSkillevent_id().equals(action))){
				skills.add(this.entries.get(i));
			}
		}
		return skills;
	}
	public ArrayList<Entry> getskillwithevent(Long event){
		ArrayList<Entry> entries = new ArrayList<Entry>();
		for(int i=0; i<this.entries.size();i++){
			if(/*(this.entries.get(i).getMyskill().getSkillevent_id()!=null)&&*/(this.entries.get(i).getMyskill().getSkillevent_action_id().equals(event))){
				entries.add(this.entries.get(i));
			}
		}
		return entries;
	}
	public ArrayList<Entry> get_entries_for_skill(Long skill_id){
		ArrayList<Entry> skill_entries = new ArrayList<Entry>();
		for(int i=0; i<this.entries.size();i++){
			if((this.entries.get(i).getMyskill().getSkillname_id()!=null)&&(this.entries.get(i).getMyskill().getSkillname_id().equals(skill_id))){
				skill_entries.add(this.entries.get(i));
			}
		}
		return skill_entries;
	}

	public ArrayList<String> getSkillname_list() {
		return skillname_list;
	}

	public Entry get_entry_for_time(Long time) {
		for(int i = 0; i<this.entries.size();i++){
			if(this.entries.get(i).getTime().equals(time)){
				return this.entries.get(i);
			}
		}
		return null;
	}
}
